package com.fruit.alipay.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.fruit.alipay.util.AlipayAppRequestBuilder;
import com.fruit.alipay.util.AlipayClientCome;
import com.fruit.alipay.util.AlipayRequestBuilder;
import com.fruit.pojo.lightpay.LightPayCashierAlipay;
import com.fruit.pojo.sys.AlipayConfig;

/**
 * 
 * @ClassName: AlipayUtil
 * @author 葛欢欢
 * @version 2017.11.30 16:40
 */
public final class AlipayUtil {
	private AlipayUtil() {
		
	}
	/**
	 * 
	 * @author 葛欢欢
	 * @version 2017.11.30 16:40
	 * @param params
	 *            params
	 * @return String
	 * @throws AlipayApiException
	 *             AlipayApiException
	 */
	public static String alipayWap(Map<String, String> params) throws AlipayApiException {
		String url = null;
		AlipayRequestBuilder builder = new AlipayRequestBuilder();
		// 封装参数
		builder.setOutTradeNo(params.get("outTradeNo")).setSubject(params.get("subject"))
				.setTotalAmount(params.get("totalAmount")).setBody(params.get("body"));

		builder.setReturnUrl(params.get("returnUrl")).setNotifyUrl(params.get("notifyUrl"))
				.setSellerId(params.get("sellerId")).setTimeoutExpress(params.get("timeOutExpress"));
		AlipayClientCome alipayClient = new AlipayClientCome(params.get("gateWayUrl"), params.get("appId"),
				params.get("appPrivateKey"), "json", params.get("charset"), params.get("alipayPublicKey"));
		AlipayTradeWapPayResponse response;
		response = alipayClient.pageExecute(builder.getRequest(), false);
		if (response != null) {
			url = response.getBody();
		}

		return url;
	}

	/**
	 * 
	* @author 葛欢欢
	* @version 2017.11.30 16:40 
	* @param params params
	* @param contextPath contextPath
	* @return String
	* @throws AlipayApiException AlipayApiException
	 */
	public static String alipayApp(Map<String, String> params, String contextPath) throws AlipayApiException {
		String url = null;
		AlipayAppRequestBuilder builder = new AlipayAppRequestBuilder();
		// 封装参数
		builder.setOutTradeNo(params.get("outTradeNo")).setSubject(params.get("subject"))
				.setTotalAmount(params.get("totalAmount")).setBody(params.get("body"))
				.setProductCode("QUICK_MSECURITY_PAY");

		builder.setNotifyUrl(params.get("notifyUrl")).setSellerId(params.get("sellerId"))
				.setTimeoutExpress(params.get("timeOutExpress"));
		AlipayClientCome alipayClient = new AlipayClientCome(params.get("gateWayUrl"), params.get("appId"),
				params.get("appPrivateKey"), "json", params.get("charset"), params.get("alipayPublicKey"));
		AlipayTradeAppPayResponse response;
		response = alipayClient.executeParams(builder.getRequest());
		if (response != null) {
			url = response.getBody();
		}

		return url;
	}

	/**
	 * 
	* @author 葛欢欢
	* @version 2017.11.30 16:40 
	* @param params params
	* @param ac ac
	* @return LightPayCashierAlipay
	* @throws AlipayApiException AlipayApiException
	 */
	public static LightPayCashierAlipay getNotifyResult(Map<String, String> params, AlipayConfig ac)
			throws AlipayApiException {

		return getNotifyResult(params, ac, null);
	}

	/**
	 * 
	* @author 葛欢欢
	* @version 2017.11.30 16:40 
	* @param params params
	* @param ac  ac
	* @param excludeKeys excludeKeys
	* @return LightPayCashierAlipay
	* @throws AlipayApiException AlipayApiException
	 */
	public static LightPayCashierAlipay getNotifyResult(Map<String, String> params, AlipayConfig ac,
			List<String> excludeKeys) throws AlipayApiException {

		LightPayCashierAlipay result = new LightPayCashierAlipay();
		if (excludeKeys != null && excludeKeys.size() > 0) {
			result.setUdfParams(new HashMap<String, String>());
			for (String key : excludeKeys) {
				if (!StringUtils.isEmpty(key)) {
					result.getUdfParams().put(key, params.remove(key));
				}
			}
		}
		try {
			if (!AlipaySignature.rsaCheckV1(params, ac.getAlipayPublicKey(), ac.getCharset())) {
				throw new AlipayApiException("验签失败，请检查支付宝公钥是否配置正确！");
			}
		} catch (AlipayApiException e) {
			throw new AlipayApiException("验签失败，请检查支付宝公钥是否配置正确！");
		}
		result.setResultType(LightPayCashierAlipay.TYPE_NOTIFY);
		result.setOutTradeNo(params.get("out_trade_no"));
		result.setTradeNo(params.get("trade_no"));
		result.setTotalAmount(params.get("total_amount"));
		result.setSellerId(params.get("seller_id"));
		result.setTradeStatus(params.get("trade_status"));
		result.setRefundFee(params.get("refund_fee"));
		result.setGmtCreate(params.get("gmt_create"));
		result.setGmtPayment(params.get("gmt_payment"));
		result.setGmtClose(params.get("gmt_close"));
		result.setGmtRefund(params.get("gmt_refund"));
		result.setBuyerId(params.get("buyer_id"));
		return result;
	}

	/**
	 * 
	* @author 葛欢欢
	* @version 2017.11.30 16:40 
	* @param params params
	* @return LightPayCashierAlipay
	* @throws AlipayApiException AlipayApiException
	 */
	public static LightPayCashierAlipay getReturnResult(Map<String, String> params) throws AlipayApiException {

		return getReturnResult(params, null);
	}

	/**
	 * 
	* @author 葛欢欢
	* @version 2017.11.30 16:40 
	* @param params params
	* @param excludeKeys excludeKeys
	* @return LightPayCashierAlipay
	* @throws AlipayApiException AlipayApiException
	 */
	public static LightPayCashierAlipay getReturnResult(Map<String, String> params, List<String> excludeKeys)
			throws AlipayApiException {

		LightPayCashierAlipay result = new LightPayCashierAlipay();
		if (excludeKeys != null && excludeKeys.size() > 0) {
			result.setUdfParams(new HashMap<String, String>());
			for (String key : excludeKeys) {
				if (!StringUtils.isEmpty(key)) {
					result.getUdfParams().put(key, params.remove(key));
				}
			}
		}
		try {
			if (!AlipaySignature.rsaCheckV1(params, params.get("alipayPublicKey"), params.get("charSet"))) {
				throw new AlipayApiException("验签失败，请检查支付宝公钥是否配置正确！");
			}
		} catch (AlipayApiException e) {
			throw new AlipayApiException("验签失败，请检查支付宝公钥是否配置正确！");
		}
		result.setResultType(LightPayCashierAlipay.TYPE_RETURN);
		result.setOutTradeNo(params.get("out_trade_no"));
		result.setTradeNo(params.get("trade_no"));
		result.setTotalAmount(params.get("total_amount"));
		result.setSellerId(params.get("seller_id"));
		return result;
	}

}
